/*
 * @Author: Turump 
 * @Date: 2021-04-07 11:55:21 
 * @Last Modified by: Turump
 * @Last Modified time: 2021-04-12 19:31:04
 * 卷边换图
 * 卷边的操作起点可以在对象之外，但touchEnd点需在节点内
 */

import { TaskBase } from "./TaskBase";

const { ccclass, property, menu, requireComponent } = cc._decorator;

@ccclass
@menu("任务组件/卷边")
@requireComponent(cc.Sprite)
export default class CurlFrameTask extends TaskBase {

    @property({ type: cc.SpriteFrame, displayName: "新图" })
    newFrame: cc.SpriteFrame = null;

    @property({ displayName: "方向向量" })
    directionVec: cc.Vec2 = cc.v2(1, 0);

    @property({ displayName: "手动指定Touch处理节点", tooltip: "用于Canvas接收不到touch的情况" })
    isDefineTouchNode = false;

    @property({ displayName: "Touch接收节点", type: cc.Node, visible() { return this.isDefineTouchNode } })
    touchNode: cc.Node = null;

    @property({type:cc.AudioClip, displayName:"音效"})
    audioClip: cc.AudioClip = null;

    //卷边手势要求的最小move距离
    minMoveDistance = 20;
    //卷边手势方向误差角度允许
    angleDeviation = 20;

    enableTask() {
        super.enableTask();
        //！！！如果有节点阻挡了canvas结点touch event，则会凉凉
        this.touchNode = this.touchNode || cc.Canvas.instance.node;
        this.touchNode.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.touchNode.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.touchNode.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.touchNode.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
    }

    stopTask() {
        super.stopTask();
        if (this.touchNode == null)
            return;
        this.touchNode.off(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.touchNode.off(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.touchNode.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.touchNode.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
    }

    //卷边操作要求有经过被卷对象
    isMovedCrossNode = false;
    touchStart(event: cc.Event.EventTouch) {
        this.isMovedCrossNode = false;
    }

    touchMove(event: cc.Event.EventTouch) {
        if(this.isMovedCrossNode == false)
            this.isMovedCrossNode = this.node.getBoundingBoxToWorld().contains(event.getLocation());
    }

    touchEnd(event: cc.Event.EventTouch) {
        //检测end点是否在本对象内
        // let isInThisNode = this.node.getBoundingBoxToWorld().contains(event.getLocation());
        let vec = event.getLocation().sub(event.getStartLocation())
        let moveDis = vec.mag();
        let angle = this.directionVec.angle(vec) * 180 / Math.PI;
        console.log(`${this.name} touch end dis:${moveDis} vec:${vec.toString()} angle:${angle} isMovedCrossNode:${this.isMovedCrossNode}`);
        if (this.isMovedCrossNode  && moveDis >= this.minMoveDistance && angle <= this.angleDeviation) {
            this.node.getComponent(cc.Sprite).spriteFrame = this.newFrame;
            this.stopTask();
            this.emitDoneEvent();
            this.audioClip && cc.audioEngine.playEffect(this.audioClip, false);
        }
    }
}
